//
//  DQEssenceViewController.m
//  DQBaisibudejie
//
//  Created by 杜倩 on 16/8/29.
//  Copyright © 2016年 杜倩. All rights reserved.
//控制器还是那个添加scrollView,scrollview上添加tabelview

#import "DQEssenceViewController.h"
#import "DQRecommendController.h"
#import "DQVideoController.h"
#import "DQPictureController.h"
#import "DQTextController.h"
#import "DQWebRedController.h"
#import "DQPaihangController.h"
#import "DQSocietyController.h"
#import "DQGameController.h"
#import "DQGirlController.h"
#import "DQColdController.h"
#import "UIBarButtonItem+buttonItem.h"
@interface DQEssenceViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *titleScroll;
//数组存放标题
@property(nonatomic,strong)NSArray *titles;

@property(nonatomic,strong)UIButton *button;

//定义按钮，记录上次选中的按钮
@property(nonatomic,strong)UIButton *Upbutton;

@property(nonatomic,strong)UIView *lineView;
//存放tabelView（内容）
@property(nonatomic,strong)UIScrollView *scrollView;
@end

@implementation DQEssenceViewController
CGFloat w= 50;//按钮的宽
//离屏幕的间距
CGFloat leftmargin = 10;


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addItem];
    //self.view.backgroundColor=[UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets=NO;//不允许自动调整scrollView内边距
    //加载控制器
    [self addChildController];
    
    //加载内容
    //1、铺scrollview-添加
    [self addScrollView];

    //加载标题栏
    [self addTitleBar];
    //添加下划线
    [self addUnderLine];
    
    //添加进来就选中某一个
    [self addSelectedTitle];
 
    //设置代理
    self.scrollView.delegate=self;
    
}

#pragma mark scrollview的代理方法
//时刻在调用
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"1234");
//}




//拖拽页面结束后，才会调用该方法--拖拽停止就会调用（拖拽完一整页才会停止）
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //计算拖拽的偏移量
    CGFloat length=scrollView.contentOffset.x;
    
    //计算是第几个view
    // NSInteger num=length/scrollView.bounds.size.width;
    NSInteger num = length/UIScreenw;
     NSLog(@"%ld",num);
    //根据下标取按钮
    //NSLog(@"%@", self.titleScroll.subviews[num]);
    [self buttonClick:self.titleScroll.subviews[num]];
    //设置标题栏的滚动
   // self.titleScroll.contentOffset = CGPointMake(num * _button.bounds.size.width, 0);
    if (num>=2) {
        self.titleScroll.contentOffset = CGPointMake((num-2) * _button.bounds.size.width, 0);
    }
   
}



/*//页面转换，按钮同时转换--只要拖拽停止就会调用该方法
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //计算拖拽的偏移量--
    CGFloat length=scrollView.contentOffset.x;
    
    //计算是第几个view
   // NSInteger num=length/scrollView.bounds.size.width;
    NSInteger num = length/UIScreenw;
   // NSLog(@"%ld",num);
    //根据下标取按钮
   //NSLog(@"%@", self.titleScroll.subviews[num]);
    [self buttonClick:self.titleScroll.subviews[num]];
    

}*/

#pragma mark 加载控制器
-(void)addChildController{
    [self addChildViewController:[[DQRecommendController alloc] init]];//推荐
     [self addChildViewController:[[DQVideoController alloc] init]];//视频
     [self addChildViewController:[[DQPictureController alloc] init]];//图片
     [self addChildViewController:[[DQTextController alloc] init]];//段子
     [self addChildViewController:[[DQWebRedController alloc] init]];//网红
     [self addChildViewController:[[DQPaihangController alloc] init]];//排行
     [self addChildViewController:[[DQSocietyController alloc] init]];//社会
     [self addChildViewController:[[DQGirlController alloc] init]];//美女
    [self addChildViewController:[[DQColdController alloc] init]];//冷知识
     [self addChildViewController:[[DQGameController alloc] init]];//游戏
    
}


#pragma mark 添加内容的scrollView
-(void)addScrollView{
    //定义设置大小
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenw, UIScreenh)];
    
    _scrollView.backgroundColor=[UIColor greenColor];//设置颜色
    //计算出加载到控制器上的子控制器的个数
    NSInteger count = self.childViewControllers.count;//个数
    for (int i=0; i<count; i++) {
        
        self.childViewControllers[i].view.frame = CGRectMake(i * UIScreenw, 0, UIScreenw, UIScreenh);
        //加载子控制器下的view
        [self.scrollView addSubview:self.childViewControllers[i].view];
    }
    //滚动的距离
    self.scrollView.contentSize = CGSizeMake(count * UIScreenw, 0);//scrollview水平滚动
    self.scrollView.pagingEnabled=YES;
    self.scrollView.showsHorizontalScrollIndicator=NO;//隐藏水平滚动
   [self.view addSubview:self.scrollView];
    
}

#pragma mark 加载标题栏
-(void)addTitleBar{
    _titleScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, UIScreenw, 35)];
    
   // _titleScroll.backgroundColor=[UIColor lightGrayColor];
    //设置颜色及透明度
    _titleScroll.backgroundColor=[[UIColor whiteColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:self.titleScroll];
    _titles = @[@"推荐",@"视频",@"图片",@"段子",@"网红",@"排行",@"社会",@"美女",@"冷知识",@"游戏"];
    CGFloat y = 0;

    CGFloat h = self.titleScroll.frame.size.height;//与titleScroll的高度一样
    
    //按钮之间的间距
    CGFloat margin= 15;
    int i=0;
    //循环创建按钮
    for (i=0; i<self.titles.count; i++) {
        _button=[UIButton buttonWithType:UIButtonTypeCustom];//自定义
        [_button setTitle:self.titles[i] forState:UIControlStateNormal];//标题
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//不选中状态下
        [_button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];//选中状态
        //[_button setBackgroundColor:[UIColor whiteColor]];
        //添加--需要设置button的frame
        _button.frame = CGRectMake(i * (w+margin)+leftmargin,y , w, h);
        [_button setBackgroundColor:[UIColor clearColor]];//清除button的颜色
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];//添加监听事件，选中为红色
        //tag值
        _button.tag=i;
        [self.titleScroll addSubview:self.button];

    }
    
    //scrllView滚动的距离
    self.titleScroll.contentSize=CGSizeMake(i*(w+margin), leftmargin*2.0);
    //隐藏水平滚动条
    self.titleScroll.showsHorizontalScrollIndicator=NO;
    
}


#pragma mark 进入界面就选中某一个标题
-(void)addSelectedTitle{
    
    [self buttonClick:self.titleScroll.subviews.firstObject];
}


#pragma mark 标题的监听事件(选中为红色)
-(void)buttonClick:(UIButton *)sender{
    //单击选中,再单击不选中
    //sender.selected = !sender.selected;
    self.Upbutton.selected = NO;//不选中
    sender.selected = YES;
    self.Upbutton = sender;//记录上次选中的按钮
    //设置下划线的颜色-跟随字体颜色改变
    self.lineView.backgroundColor = [sender titleColorForState:UIControlStateSelected];//选中
    
    //切换标题的时候，下面的scrollview页面也跳转---即改变contentOffset--x,y
    //根据按钮的tag值算
    self.scrollView.contentOffset = CGPointMake(sender.tag*UIScreenw, 0);
    
    
    //动画
    [UIView animateWithDuration:0.2 animations:^{
        //改变字体的宽度---设置滑动条的大小（根据字的多少改变）
       /* //获得当前字体的宽度
        NSMutableDictionary *dict=[NSMutableDictionary dictionary];
        dict[NSFontAttributeName] = [UIFont systemFontOfSize:15];
        CGFloat textW = [sender.currentTitle sizeWithAttributes:dict].width;*/
        CGFloat textW = [sender.currentTitle sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}].width;
        self.lineView.bounds = CGRectMake(0, 0, textW, 3);
        //改变line的坐标--改变x,y
        self.lineView.center = CGPointMake(sender.center.x,_titleScroll.bounds.size.height);
    }];
}



#pragma mark 添加下划线
-(void)addUnderLine{
    _lineView=[[UIView alloc] init];
    //设置大小
    CGFloat lineW=w;
    CGFloat lineH=3;
    CGFloat lineY= self.titleScroll.bounds.size.height- lineH;//scrollview的高-自身的高
    CGFloat lineX=leftmargin;
    _lineView.frame=CGRectMake(lineX, lineY, lineW, lineH);
    [self.titleScroll addSubview:_lineView];
}



#pragma mark 添加导航栏标题
-(void)addItem{
    //设置左边🏆
    UIBarButtonItem *itemL=[UIBarButtonItem barButtonItemWith:[UIButton buttonWithType:UIButtonTypeCustom] addTarget:self action:@selector(leftClick) andImage:@"nav_item_game_icon_20x20_" andhightLighted:@"nav_item_game_click_icon_20x20_"];
    self.navigationItem.leftBarButtonItem=itemL;

 //-------------右边-------------
//设置右边的穿越键
    UIBarButtonItem *itemR=[UIBarButtonItem barButtonItemWith:[UIButton buttonWithType:UIButtonTypeCustom] addTarget:self action:@selector(rightClick) andImage:@"navigationButtonRandom_26x26_" andhightLighted:@"navigationButtonRandomClick_26x26_"];

    self.navigationItem.rightBarButtonItem=itemR;

    //设置中间的图片--标题
    UIImageView *imgView=[[UIImageView alloc] init];
    imgView.image=[UIImage imageNamed:@"MainTitle"];
    [imgView sizeToFit];//自动适应尺寸
    //UIImageView *imgView= [UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"];
    self.navigationItem.titleView=imgView;
}

#pragma mark 左按钮的单击事件
-(void)leftClick{
    NSLog(@"%s",__func__);//可以输出那个方法
}

#pragma mark 右按钮的单击事件
-(void)rightClick{
    NSLog(@"%s",__func__);
}

@end
